#include "crypto_utils.h"

int main() {
    EVP_PKEY *alice_priv = NULL, *bob_pub = NULL;
    DigitalEnvelope envelope = {0};
    unsigned char *plaintext = NULL;
    size_t plaintext_len;
    FILE *file = NULL;
    int ret = 0;

    printf("=== Alice (20231402lsh) - Sender ===\n\n");

    // 加载Alice的私钥和Bob的公钥
    alice_priv = load_private_key(ALICE_PRIVATE_KEY);
    if (!alice_priv) {
        fprintf(stderr, "Failed to load Alice's private key\n");
        return -1;
    }

    bob_pub = load_public_key(BOB_PUBLIC_KEY);
    if (!bob_pub) {
        fprintf(stderr, "Failed to load Bob's public key\n");
        EVP_PKEY_free(alice_priv);
        return -1;
    }

    // 读取明文文件
    file = fopen("plain.txt", "rb");
    if (!file) {
        printf("Creating plain.txt with default content...\n");
        file = fopen("plain.txt", "wb");
        if (file) {
            const char *default_content = "20231402lsh - Alice\n";
            fwrite(default_content, 1, strlen(default_content), file);
            fclose(file);
            file = fopen("plain.txt", "rb");
        }
    }

    if (!file) {
        fprintf(stderr, "Cannot open plain.txt\n");
        goto cleanup;
    }

    fseek(file, 0, SEEK_END);
    plaintext_len = ftell(file);
    fseek(file, 0, SEEK_SET);

    plaintext = malloc(plaintext_len);
    if (!plaintext) {
        fprintf(stderr, "Memory allocation failed\n");
        fclose(file);
        goto cleanup;
    }

    fread(plaintext, 1, plaintext_len, file);
    fclose(file);

    printf("Plaintext (%zu bytes): %.*s\n", plaintext_len, (int)plaintext_len, plaintext);

    // 创建数字信封
    printf("\nCreating digital envelope...\n");
    if (create_digital_envelope(plaintext, plaintext_len, alice_priv, bob_pub, &envelope) != 1) {
        fprintf(stderr, "Failed to create digital envelope\n");
        goto cleanup;
    }

    // 保存数字信封到文件
    file = fopen("envelope.dat", "wb");
    if (!file) {
        fprintf(stderr, "Cannot open envelope.dat for writing\n");
        goto cleanup;
    }

    // 写入密文长度和密文
    fwrite(&envelope.ciphertext_len, sizeof(size_t), 1, file);
    fwrite(envelope.ciphertext, 1, envelope.ciphertext_len, file);

    // 写入加密密钥长度和加密密钥
    fwrite(&envelope.encrypted_key_len, sizeof(size_t), 1, file);
    fwrite(envelope.encrypted_key, 1, envelope.encrypted_key_len, file);

    // 写入签名长度和签名
    fwrite(&envelope.signature_len, sizeof(size_t), 1, file);
    fwrite(envelope.signature, 1, envelope.signature_len, file);

    fclose(file);

    printf("\n✓ Digital envelope created successfully!\n");
    printf("  Ciphertext length: %zu bytes\n", envelope.ciphertext_len);
    printf("  Encrypted key length: %zu bytes\n", envelope.encrypted_key_len);
    printf("  Signature length: %zu bytes\n", envelope.signature_len);
    printf("  Envelope saved to: envelope.dat\n");

    ret = 1;

cleanup:
    if (plaintext) free(plaintext);
    free_digital_envelope(&envelope);
    if (alice_priv) EVP_PKEY_free(alice_priv);
    if (bob_pub) EVP_PKEY_free(bob_pub);
    
    return ret ? 0 : -1;
}
